package com.allan.springboot_wms.service;


import com.allan.springboot_wms.domain.Out;
import com.allan.springboot_wms.domain.Out;
import com.allan.springboot_wms.domain.OutExample;
import com.allan.springboot_wms.mapper.OutMapper;
import com.allan.springboot_wms.req.OutQueryReq;
import com.allan.springboot_wms.req.OutSaveReq;
import com.allan.springboot_wms.resp.CommonResp;
import com.allan.springboot_wms.resp.OutResp;
import com.allan.springboot_wms.resp.PageResp;
import com.allan.springboot_wms.util.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class OutService {
    //导入日志
    private static final Logger LOG = LoggerFactory.getLogger(OutService.class);

    @Resource
    private OutMapper outMapper;

    public PageResp<OutResp> list(OutQueryReq req) {
        //mybatis的逆向工程中会生成实例及实例对应的example，example用于添加条件，相当于where后面的部分
        //比如criteria.andXxxEqualTo(value)，添加Xxx字段等于value的条件
        OutExample outExample = new OutExample();
        OutExample.Criteria criteria = outExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getOut_name())) {  //判断是否为空 不为空有名字的话 按名字来查找
            criteria.andOutNameLike("%" + req.getOut_name() + "%");
        }
        PageHelper.startPage(req.getPage(), req.getSize());
        //先转成out类然后再遍历得到OutResp
        List<Out> outList = outMapper.selectByExample(outExample);
        //pageinfo放入得到总页数或总行数,并在日志中打印出
        PageInfo<Out> pageInfo = new PageInfo<>(outList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());    //一般传给前端传总行数就可
        // 列表复制
        List<OutResp> list = CopyUtil.copyList(outList, OutResp.class);
        //把返回的列表添加到PageResp
        PageResp<OutResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    public Out selectById(Integer id, String name){
        return outMapper.selectByPrimaryKey(id, name);
    }

    public void delete(Integer out_num, String out_name){
        outMapper.deleteByPrimaryKey(out_num, out_name);
    }

    public void save(OutSaveReq req){
        Out out = CopyUtil.copy(req, Out.class);
        if(ObjectUtils.isEmpty(req.getOutNum())){
            //新增
//            out.setId((int) snowFlake.nextId()); //自己生成ID
            outMapper.insert(out);
        }else{
            //更新
            outMapper.updateByPrimaryKey(out);
        }
    }
    //更新操作
    public void update(OutSaveReq req){
        Out out = CopyUtil.copy(req, Out.class);
        if(!ObjectUtils.isEmpty(req.getOutNum())){
            outMapper.updateByOutNum(out);
        }
    }

    // 计算利润总和操作
    public BigDecimal addSumProfit(){

        return outMapper.sumProfit();
    }
}
